// 初始阿虎配置项的默认值
// Vue.extend();
import Vue from 'vue';
import message from './message';
// 导入的此处message对象是message组件的配置项

// 把message的配置项设置为Message类(就是Vue类)的默认配置项
const Msg = Vue.extend(message);

function $message(option = {}) {
  // 接收参数传递给Message类 去覆盖默认值：比如提示的文字、自动关闭的时间、主题颜色
  const msg = new Msg({
    data() {
      return {
        message: option.message,
        duration: option.duration || 3000,
        type: option.type || 'success',
        center: option.center || false,
      };
    },
    methods: {
      hide() {
        if (option.duration === 0) {
          this.visible = true;
        } else {
          this.visible = false;
        }
      },
    },
  });
  // 模板编译到实例的$el属性
  msg.$mount();
  // 手动挂载到body
  document.body.appendChild(msg.$el);
}

// eslint-disable-next-line func-names
export const Message = function (option = {}) {
  const msg = new Msg({
    data() {
      return {
        message: option.message,
        duration: option.duration || 3000,
        type: option.type || 'success',
        center: option.center || false,
      };
    },
    methods: {
      hide() {
        if (option.duration === 0) {
          this.visible = true;
        } else {
          this.visible = false;
        }
      },
    },
  });
  // 用extend出来的实例都需要手动挂载
  msg.$mount();
  document.body.appendChild(msg.$el);
};


export default {
  // eslint-disable-next-line no-shadow
  install(Vue) {
    // eslint-disable-next-line no-param-reassign
    Vue.prototype.$message = $message;
  },
};
